<?php

use yii\db\Migration;

/**
 * Handles adding columns to table `{{%contract}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%contract_purpose}}`
 */
class m221113_150333_add_id_purpose_column_to_contract_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->addColumn('{{%contract}}', 'id_purpose', $this->integer());

        // creates index for column `id_purpose`
        $this->createIndex(
            '{{%idx-contract-id_purpose}}',
            '{{%contract}}',
            'id_purpose'
        );

        // add foreign key for table `{{%contract_purpose}}`
        $this->addForeignKey(
            '{{%fk-contract-id_purpose}}',
            '{{%contract}}',
            'id_purpose',
            '{{%contract_purpose}}',
            'id',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%contract_purpose}}`
        $this->dropForeignKey(
            '{{%fk-contract-id_purpose}}',
            '{{%contract}}'
        );

        // drops index for column `id_purpose`
        $this->dropIndex(
            '{{%idx-contract-id_purpose}}',
            '{{%contract}}'
        );

        $this->dropColumn('{{%contract}}', 'id_purpose');
    }
}
